package businessLogic.transaction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alexander
 */
public class JDBCTransaction implements Transaction<Connection> {

    String url = "jdbc:derby://localhost:1527/sample";
    String username = "app";
    String password = "app";
    Connection conexion;

    public JDBCTransaction() {
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(JDBCTransaction.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }    
    
    @Override
    public boolean createTransaction() {
        try {
            conexion = DriverManager.getConnection(url, username, password);
        } catch (SQLException ex) {
            Logger.getLogger(JDBCTransaction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }

    @Override
    public void commit() {
        try {
            conexion.commit();
        } catch (SQLException ex) {
            Logger.getLogger(JDBCTransaction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void close() {
        try {
            conexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(JDBCTransaction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void rollBack() {
        try {
            conexion.rollback();
        } catch (SQLException ex) {
            Logger.getLogger(JDBCTransaction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Connection getTransaction() {
        return conexion;
    }
    
}
